<template>
  <div class="app-container">
    <el-container>
      <el-header>
        <div class="header-content">
          <div class="logo">
            <router-link to="/">投票系统</router-link>
          </div>
          <el-menu
            mode="horizontal"
            :router="true"
            :default-active="activeRoute"
          >
            <el-menu-item index="/">首页</el-menu-item>
            <el-menu-item index="/votes">投票</el-menu-item>
            <el-menu-item index="/news">新闻</el-menu-item>
            <el-menu-item index="/announcements">公告</el-menu-item>
          </el-menu>
          <div class="user-actions">
            <template v-if="isAuthenticated">
              <el-dropdown>
                <el-button type="primary">
                  个人中心<el-icon class="el-icon--right"><arrow-down /></el-icon>
                </el-button>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item @click="$router.push('/profile')">个人信息</el-dropdown-item>
                    <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
            <template v-else>
              <el-button @click="$router.push('/login')">登录</el-button>
              <el-button type="primary" @click="$router.push('/register')">注册</el-button>
            </template>
          </div>
        </div>
      </el-header>

      <el-main>
        <router-view></router-view>
      </el-main>

      <el-footer>
        <div class="footer-content">
          <p>&copy; 2025 投票系统 All Rights Reserved.</p>
        </div>
      </el-footer>
    </el-container>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useStore } from 'vuex'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowDown } from '@element-plus/icons-vue'

const store = useStore()
const route = useRoute()
const router = useRouter()

const isAuthenticated = computed(() => store.getters.isAuthenticated)
const activeRoute = computed(() => route.path)

const handleLogout = () => {
  store.commit('clearAuth')
  ElMessage.success('已退出登录')
  router.push('/login')
}
</script>

<style>
.app-container {
  min-height: 100vh;
}

.el-container {
  min-height: 100vh;
}

.el-header {
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: fixed;
  width: 100%;
  z-index: 1000;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
}

.logo {
  font-size: 24px;
  font-weight: bold;
}

.logo a {
  color: #409EFF;
  text-decoration: none;
}

.el-main {
  padding-top: 80px;
  background-color: #f5f7fa;
}

.user-actions {
  display: flex;
  gap: 10px;
}

.footer-content {
  text-align: center;
  color: #666;
  padding: 20px 0;
}

.el-menu {
  border-bottom: none;
}

.el-menu--horizontal {
  border-bottom: none;
}
</style>
